package com.cqvie.service;

import java.lang.annotation.Documented;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cqvie.controller.msg.FindAttendMsg;
import com.cqvie.controller.msg.FindLeaveMsg;
import com.cqvie.controller.msg.StuLoginMsg;
import com.cqvie.dao.AttendDao;
import com.cqvie.dao.AttendDetailsDao;
import com.cqvie.dao.CommonDao;
import com.cqvie.dao.CourseScheduleDao;
import com.cqvie.dao.LeaveMessageDao;
import com.cqvie.dao.StudentDao;
import com.cqvie.model.AttendDetails;
import com.cqvie.model.LeaveMessage;
import com.cqvie.model.Role;
import com.cqvie.tools.Note;

@Transactional
@Service("stuService")

public class StudentService {
	@Resource
	private CourseScheduleDao csDao;
	@Resource
	private StudentDao stuDao;
	@Resource
	private AttendDao attendDao;
	@Resource
	private LeaveMessageDao leaveMsgDao;
	@Resource
	private AttendDetailsDao adDao;
	
	@Resource
	private CommonDao commonDao;
	//学生登录功能
	/**
	 * 如果返回为0，说明登录失败
	 * 如果返回为1，学生的普通登录成功，跳转到学生功能界面
	 * 如果返回为2，学生带有教师部分权限的登录成功，跳转到教师功能界面
	 * */
	@Note("学生登录功能:如果返回为0，说明登录失败;"
			+ "如果返回为1，学生的普通登录成功，跳转到学生功能界面;"
			+ "如果返回为2，学生带有教师部分权限的登录成功，跳转到教师功能界面")
	public String stuloginCompetence(StuLoginMsg slm,HttpSession session){
		String end="0";
		boolean mody1=csDao.isExistStuID(slm.getStuID());
		boolean mody2=stuDao.isExistStu(slm.getStuID(), slm.getStuName());
		if(mody1&&mody2){
			end="2";
			session.setAttribute("jurisdiction",((Role)(commonDao.findObjectByHQL("FROM Role where description='student_teacher'"))).getName());
			session.setAttribute("username", slm.getStuID());
		}else if(mody2&&!mody1){
			end="1";
			session.setAttribute("jurisdiction",((Role)(commonDao.findObjectByHQL("FROM Role where description='student'"))).getName());
			session.setAttribute("username", slm.getStuID());
		}
		return end;
	}
	
	//根据学生来查询考勤记录
	public List<AttendDetails> findstuAttendDateils(FindAttendMsg fam) {
		List<AttendDetails> attends = adDao.selectBystuName(fam.getStuName());
		if (attends!=null) {
			return attends;
		} else
			return null;
	}

	//查询请假记录
	public List<LeaveMessage> findstuMsg(FindLeaveMsg flm) {
		List<LeaveMessage> lms = leaveMsgDao.selectByIDLeaveMsg(flm.getLeavename());
		if (lms!=null) {
			return lms;
		} else
			return null;
	}
}
